﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Fcom.VNInsurance.Web.Models;

namespace Fcom.VNInsurance.Web.Controllers
{
     [Authorize(Roles = "13,14")]
    public class BenefitController : Fcom.VNInsurance.Web.CultureHelper.CultureController
    {
        //
        // GET: /Benefit/
        Fcom.VNInsurance.Web.Models.VNInsuranceDataContext db = new Models.VNInsuranceDataContext();
        public ActionResult Index()
        {
            return View();
        }
        [Authorize(Roles = "14")]
        public ActionResult MNBenefit(int id)
        {
            ViewBag.coveid = id;
            ViewBag.coverage = db.Coverages.Where(m => m.ID == id).FirstOrDefault();
            return View();
        }
        [HttpGet]
        public ActionResult AddBenefit()
        {
            ViewBag.Coverage = (from a in db.Coverages select a);
            return View();
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult AddBenefit(BaseBenefitLang com)
        {
            Benefit bn = new Benefit();
            BenefitLang bnlang = new BenefitLang();
            BenefitLang bnlang1 = new BenefitLang();
            var isexit = db.Benefits.Where(m => m.BenefitName == com.BenefitName).FirstOrDefault();
            if (isexit == null)
            {
                bn.BenefitName = com.BenefitName;
                bn.CoverageID = com.CoverageID;
                bn.ParentID = com.ParentID;
                bn.InputKey = com.InputKey;
                db.Benefits.InsertOnSubmit(bn);
                db.SubmitChanges();
                bnlang.BenefitID = bn.ID;
                bnlang.DisplayName = com.BenefitName;
                bnlang.Detail = com.Detail;
                bnlang.EnableSub = com.EnableSubMoney;
                bnlang.LangID = Request.Cookies["_culture"]["lang"];
                bnlang.EnableInsMoney = com.EnableInsMoney;
                db.BenefitLangs.InsertOnSubmit(bnlang);
                if (bnlang.LangID == "en-US")
                {
                    bnlang1.LangID = "vi-VN";
                }
                else
                {
                    bnlang1.LangID = "en-US";
                }
                bnlang1.BenefitID = bn.ID;
                bnlang1.DisplayName = com.BenefitName;
                bnlang1.EnableSub = com.EnableSubMoney;
                bnlang1.EnableInsMoney = com.EnableInsMoney;
                db.BenefitLangs.InsertOnSubmit(bnlang1);
                db.SubmitChanges();
                return RedirectToAction("MNBenefit", new { id=com.CoverageID});
            }
            else
            {
                return RedirectToAction("MNBenefit", new { id = com.CoverageID });
            }
        }
        [Authorize(Roles = "14")]
        public ActionResult getBenefitList(int CoverageID)
        {
            var benlang = db.BenefitLangs.Where(m =>((m.LangID == Request.Cookies["_culture"]["lang"])&&( m.Benefit.CoverageID==CoverageID))).Select(m => new {
                BenefitID = m.BenefitID,
                LangID=m.LangID,
                BenefitCoverageName=m.Benefit.Coverage.Name,
                BenefitDisplay=m.DisplayName,
                BenefitEnadbleSub=m.EnableSub,
                BenefitEnableIsmoney=m.EnableInsMoney,
                m.EnableChildBenefit,
                m.EnableNumJoin,
                m.EnableText
               
            });
            return Json(benlang, JsonRequestBehavior.AllowGet);
        }
        [Authorize(Roles = "14")]
        [HttpGet]
        public ActionResult EditBenefit(int id)
        {
            var langid=Request.Cookies["_culture"]["lang"];
            var car = db.BenefitLangs.Where(m => m.BenefitID == id && m.LangID ==langid ).FirstOrDefault();
            ViewBag.Coverage = (from a in db.Coverages select a);
                BaseBenefitLang basebenefit = new BaseBenefitLang();
                basebenefit.BenefitID = car.BenefitID;
                basebenefit.BenefitName = car.Benefit.BenefitName;
                basebenefit.Displayname = car.DisplayName;
                basebenefit.InputKey = car.Benefit.InputKey;
                basebenefit.ParentID =car.Benefit.ParentID;
                basebenefit.LangID = car.LangID;
                basebenefit.CoverageID = car.Benefit.CoverageID;
                basebenefit.Detail =HttpUtility.HtmlDecode(car.Detail);
                basebenefit.EnableInsMoney = car.EnableInsMoney;
                basebenefit.EnableSubMoney = car.EnableSub;
                return View(basebenefit);
        }
        [Authorize(Roles = "14")]
        public ActionResult GetBeneParent(int id)
        {
            var langid = Request.Cookies["_culture"]["lang"];
            var a=(from b in db.BenefitLangs where b.LangID == langid && b.Benefit.CoverageID==id select new { b.BenefitID,b.DisplayName});
            return Json(a,JsonRequestBehavior.AllowGet);
        }
        [Authorize(Roles = "14")]
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult EditBenefit(BaseBenefitLang benefit)
        {
            var benlang = db.BenefitLangs.Where(m => m.BenefitID == benefit.BenefitID && m.LangID == benefit.LangID).FirstOrDefault();
            if (ModelState.IsValid)
            {
                if (benlang != null)
                {
                    benlang.DisplayName = benefit.Displayname;
                    benlang.Detail =HttpUtility.HtmlEncode(benefit.Detail);
                    benlang.Benefit.ParentID = benefit.ParentID;
                    benlang.Benefit.InputKey = benefit.InputKey;
                    benlang.EnableSub = benefit.EnableSubMoney;
                    benlang.EnableInsMoney = benefit.EnableInsMoney;
                    benlang.Benefit.CoverageID = benefit.CoverageID;
                    db.SubmitChanges();
                    return RedirectToAction("MNBenefit", new { id = benefit.CoverageID });
                }
                else
                {
                    return RedirectToAction("MNBenefit", new { id = benefit.CoverageID });
                }
            }
            else
            {
                ViewBag.Coverage = (from a in db.Coverages select a);
                return View(benefit);
            }
        }
        [HttpPost]
        public ActionResult remove(int id)
        {
            var at = db.BenefitLangs.Where(m => m.BenefitID == id);
            db.BenefitLangs.DeleteAllOnSubmit(at);
            var ben = db.Benefits.Where(m => m.ID == id).SingleOrDefault();
        
            db.Benefits.DeleteOnSubmit(ben);
            db.SubmitChanges();
            return Json(true);
        }
    }
}
